// Jest测试环境设置
import * as fs from 'fs-extra';
import * as path from 'path';

// 创建测试用的临时目录
const TEST_TEMP_DIR = path.join(__dirname, 'temp');

beforeAll(async () => {
  // 确保测试临时目录存在
  await fs.ensureDir(TEST_TEMP_DIR);
});

afterAll(async () => {
  // 清理测试临时文件
  try {
    await fs.remove(TEST_TEMP_DIR);
  } catch (error) {
    console.warn('清理测试文件失败:', error);
  }
});

// 导出测试工具函数
export const createTestJavaFile = async (fileName: string, content: string): Promise<string> => {
  const filePath = path.join(TEST_TEMP_DIR, fileName);
  await fs.writeFile(filePath, content, 'utf-8');
  return filePath;
};

export const createTestSpringBootProject = async (projectName: string): Promise<string> => {
  const projectPath = path.join(TEST_TEMP_DIR, projectName);
  await fs.ensureDir(projectPath);
  await fs.ensureDir(path.join(projectPath, 'src', 'main', 'java'));
  await fs.ensureDir(path.join(projectPath, 'src', 'test', 'java'));
  
  // 创建基本的pom.xml
  const pomContent = `<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>${projectName}</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>`;
  
  await fs.writeFile(path.join(projectPath, 'pom.xml'), pomContent, 'utf-8');
  return projectPath;
};

export const TEST_JAVA_SAMPLES = {
  LARGE_FILE: `
package com.test;

import java.util.*;
import java.io.*;

public class LargeClass {
    ${Array.from({length: 30}, (_, i) => `
    public void method${i}() {
        // 方法 ${i}
        ${Array.from({length: 20}, (_, j) => `        System.out.println("line ${j}");`).join('\n')}
    }`).join('\n')}
}`,

  CONTROLLER_WITH_REPOSITORY: `
package com.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.test.repository.UserRepository;

@RestController
@RequestMapping("/api/users")
public class UserController {
    
    @Autowired
    private UserRepository userRepository;
    
    @GetMapping
    public List<User> getUsers() {
        return userRepository.findAll();
    }
}`,

  SERVICE_WITHOUT_TRANSACTION: `
package com.test.service;

import org.springframework.stereotype.Service;
import com.test.repository.UserRepository;

@Service
public class UserService {
    
    private UserRepository userRepository;
    
    public void saveUser(User user) {
        userRepository.save(user);
    }
    
    public void deleteUser(Long id) {
        userRepository.deleteById(id);
    }
}`,

  JAVA8_OPTIMIZATION_NEEDED: `
package com.test;

import java.util.*;

public class OldStyleCode {
    
    public List<String> processNames(List<User> users) {
        List<String> result = new ArrayList<>();
        for (User user : users) {
            if (user != null && user.getName() != null) {
                result.add(user.getName().toUpperCase());
            }
        }
        return result;
    }
    
    public void sortUsers(List<User> users) {
        Collections.sort(users, new Comparator<User>() {
            @Override
            public int compare(User a, User b) {
                return a.getName().compareTo(b.getName());
            }
        });
    }
}`
};